﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VVA.Data.Entity;
using VVA.Data.Service;

namespace VVA.Website.UI.Areas.Admin.Controllers
{
    [Authorize]
    public class TaxonomyController : Controller
    {
        //
        // GET: /Admin/Taxonomy/

        public ActionResult Index(string table)
        {
            var service = new TaxonomyService();
            var model = service.GetAll(table, "vi-VN");
            ViewBag.Table = table;
            return View(model);
        }

        public ActionResult Create(string table)
        {
            ViewBag.TableName = table;
            var service = new TaxonomyService();
            ViewBag.Taxonomy = new SelectList(service.GetAll(table, "vi-VN"), "TaxonomyID", "TaxonomyName");
            return View();
        }

        [HttpPost]
        public ActionResult Create(Taxonomy model)
        {
            try
            {
                var service = new TaxonomyService();
                model.Culture = "vi-VN";
                model.TaxonomyID = Guid.NewGuid();
                service.Insert(model);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return new EmptyResult();
        }

        public ActionResult Edit(string table, Guid id)
        {
            try
            {
                ViewBag.TableName = table;
                var service = new TaxonomyService();
                ViewBag.Taxonomy = new SelectList(service.GetAll(table, "vi-VN"), "TaxonomyID", "TaxonomyName");
                var model = service.GetByID(id);
                return View(model);
            }
            catch (Exception)
            {
                throw;
            }
        }

        [HttpPost]
        public ActionResult Edit(Taxonomy model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var service = new TaxonomyService();
                    model.Culture = "vi-VN";
                    service.Update(model);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return new EmptyResult();
        }


        [HttpPost]
        public ActionResult Delete(Guid id)
        {
            try
            {
                var service = new TaxonomyService();
                service.Delete(new Taxonomy() { TaxonomyID = id });
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return new EmptyResult();
        }

    }
}
